1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  
21  import com.google.common.base.Function;
22  import com.google.common.base.Predicate;
23  import com.google.common.collect.Maps.EntryTransformer;
24  
25  import java.io.Serializable;
26  import java.util.AbstractSet;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.Map;
30  import java.util.Set;
31  import java.util.SortedMap;
32  import java.util.SortedSet;
33  
34  import javax.annotation.Nullable;
35  
36  /**
37   * Minimal GWT emulation of {@code com.google.common.collect.Platform}.
38   *
39   * <p><strong>This .java file should never be consumed by javac.</strong>
40   *
41   * @author Hayward Chan
42   */
43  final class Platform {
44  
45    static <T> T[] newArray(T[] reference, int length) {
46      return GwtPlatform.newArray(reference, length);
47    }
48    
49    /*
50     * Regarding newSetForMap() and SetFromMap:
51     *
52     * Written by Doug Lea with assistance from members of JCP JSR-166
53     * Expert Group and released to the public domain, as explained at
54     * http://creativecommons.org/licenses/publicdomain
55     */
56    
57    static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
58      return new SetFromMap<E>(map);
59    }
60  
61    private static class SetFromMap<E> extends AbstractSet<E>
62        implements Set<E>, Serializable {
63      private final Map<E, Boolean> m; // The backing map
64      private transient Set<E> s; // Its keySet
65  
66      SetFromMap(Map<E, Boolean> map) {
67        checkArgument(map.isEmpty(), "Map is non-empty");
68        m = map;
69        s = map.keySet();
70      }
71  
72      @Override public void clear() {
73        m.clear();
74      }
75      @Override public int size() {
76        return m.size();
77      }
78      @Override public boolean isEmpty() {
79        return m.isEmpty();
80      }
81      @Override public boolean contains(Object o) {
82        return m.containsKey(o);
83      }
84      @Override public boolean remove(Object o) {
85        return m.remove(o) != null;
86      }
87      @Override public boolean add(E e) {
88        return m.put(e, Boolean.TRUE) == null;
89      }
90      @Override public Iterator<E> iterator() {
91        return s.iterator();
92      }
93      @Override public Object[] toArray() {
94        return s.toArray();
95      }
96      @Override public <T> T[] toArray(T[] a) {
97        return s.toArray(a);
98      }
99      @Override public String toString() {
100       return s.toString();
101     }
102     @Override public int hashCode() {
103       return s.hashCode();
104     }
105     @Override public boolean equals(@Nullable Object object) {
106       return this == object || this.s.equals(object);
107     }
108     @Override public boolean containsAll(Collection<?> c) {
109       return s.containsAll(c);
110     }
111     @Override public boolean removeAll(Collection<?> c) {
112       return s.removeAll(c);
113     }
114     @Override public boolean retainAll(Collection<?> c) {
115       return s.retainAll(c);
116     }
117   }
118 
119   static MapMaker tryWeakKeys(MapMaker mapMaker) {
120     return mapMaker;
121   }
122 
123   static <K, V1, V2> SortedMap<K, V2> mapsTransformEntriesSortedMap(
124       SortedMap<K, V1> fromMap,
125       EntryTransformer<? super K, ? super V1, V2> transformer) {
126     return Maps.transformEntriesIgnoreNavigable(fromMap, transformer);
127   }
128 
129   static <K, V> SortedMap<K, V> mapsAsMapSortedSet(
130       SortedSet<K> set, Function<? super K, V> function) {
131     return Maps.asMapSortedIgnoreNavigable(set, function);
132   }
133 
134   static <E> SortedSet<E> setsFilterSortedSet(
135       SortedSet<E> unfiltered, Predicate<? super E> predicate) {
136     return Sets.filterSortedIgnoreNavigable(unfiltered, predicate);
137   }
138   
139   static <K, V> SortedMap<K, V> mapsFilterSortedMap(
140       SortedMap<K, V> unfiltered, Predicate<? super Map.Entry<K, V>> predicate) {
141     return Maps.filterSortedIgnoreNavigable(unfiltered, predicate);
142   }
143 
144   private Platform() {}
145 }